﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Request.ReduceFee;
using Common.Contract.Customer.Response;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Customer.Response.ReduceFee;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Common.Service.DataTables;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Filters;
using SMECustomerWebsite.Models.ViewModels.CustomerManager;
using SMECustomerWebsite.Models.ViewModels.ExportData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class CustomerManagerController : ControllerBase
    {
        #region Customer
        [Authorize]
        [CustomActionFilter]
        public ActionResult TotalCustomer()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult TotalCustomerData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            var customerRecords = new RetrieveCustomerResponse();
            List<object[]> listCust = new List<object[]>();

            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.CusStatus = pageRequest.SearchColumns[8];
                    filter.BusinessStatus = pageRequest.SearchColumns[9];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.CusStatus = pageRequest.SearchColumns[7];
                    filter.BusinessStatus = pageRequest.SearchColumns[8];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.CusStatus = pageRequest.SearchColumns[6];
                    filter.BusinessStatus = pageRequest.SearchColumns[7];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.CusStatus = pageRequest.SearchColumns[4];
                    filter.BusinessStatus = pageRequest.SearchColumns[5];
                    break;
                default:
                    break;
            }
            customerRecords = RetrieveAllCustomer(filter, pageRequest);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.CusStatus,
                                                             x.BusinessStatus.Equals("1")?"Active":"Inactive"
                        }).ToList();
                    break;
                case "3":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.CusStatus,
                                                             x.BusinessStatus.Equals("1")?"Active":"Inactive"
                        }).ToList();
                    break;
                case "2":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.CusStatus,
                                                             x.BusinessStatus.Equals("1")?"Active":"Inactive"
                        }).ToList();
                    break;
                case "1":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.CusStatus,
                                                             x.BusinessStatus.Equals("1")?"Active":"Inactive"
                        }).ToList();
                    break;
                default:
                    break;
            }
            pageResponse = new DataTableResponse
                    {
                        sEcho = pageRequest.Echo,
                        iTotalRecords = customerRecords.Page.TotalRecords,
                        sColumns = pageRequest.ColumnNames,
                        aaData = listCust
                    };
            return Json(pageResponse);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult ExportDataTotalCustomer()
        {
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = new FilterRecord
                    {
                        Branch = CurrentUserRecord.BranchId,
                        DaoName = CurrentUserRecord.Position.Equals("SMEM") ? string.Empty : string.Join(",", GetListDaoManager().ToArray())
                    };
            customerRecords = RetrieveAllCustomer(filter);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ExportCustomerViewModel>() : customerRecords.Records.Select(x => new ExportCustomerViewModel
            {
                Dao = x.Dao,
                StaffName = x.DaoName,
                Cif = x.CIF,
                CustName = x.Name,
                CustStatus = x.CusStatus,
                Industry = x.VpbIndustry,
                SubSegment = x.SubSegment,
                CustomerType = x.CustType
            }).ToList();
            DataTable dt = new DataTable();
            dt.TableName = "Total Customers ";
            var fileName = "Total Customers SME " + CurrentUserRecord.Branch + ".xlsx";
            dt = ToDataTable(resultCustomer);
            ExportToExcel(dt, fileName);
            return RedirectToAction("TotalCustomer", "CustomerManager");
        }

        #endregion
        #region Customer Portfolio
        [CustomActionFilter]
        [Authorize]
        public ActionResult CustomerPortfolio()
        {
            CustomerPortfolioViewModel listCountCustomer = CountCustomerByGroup();
            listCountCustomer.CurrentUser = this.CurrentUserRecord;
            return View(listCountCustomer);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustomerPortfolioData(DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            DataTableResponse pageResponse = null;
            List<object[]> listCust = new List<object[]>();
            int totalRecord = 0;
            switch (type)
            {
                case "deposit":
                    Thread.Sleep(100);
                    listCust = CreateDatatableDeposit(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_deposit":
                    Thread.Sleep(100);
                    listCust = CreateDatatableDeposit(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "loan":
                    Thread.Sleep(200);
                    listCust = CreateDatatableLoan(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_loan":
                    Thread.Sleep(200);
                    listCust = CreateDatatableLoan(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "card":
                    Thread.Sleep(300);
                    listCust = CreateDatatableCard(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_card":
                    Thread.Sleep(300);
                    listCust = CreateDatatableCard(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "pos":
                    Thread.Sleep(400);
                    listCust = CreateDatatablePos(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_pos":
                    Thread.Sleep(400);
                    listCust = CreateDatatablePos(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "i2b":
                    Thread.Sleep(500);
                    listCust = CreateDatatableI2b(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_i2b":
                    Thread.Sleep(500);
                    listCust = CreateDatatableI2b(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "trade":
                    Thread.Sleep(600);
                    listCust = CreateDatatableTrade(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_trade":
                    Thread.Sleep(600);
                    listCust = CreateDatatableTrade(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "chuoi":
                    Thread.Sleep(700);
                    listCust = CreateDatatableChuoi(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                case "new_chuoi":
                    Thread.Sleep(700);
                    listCust = CreateDatatableChuoi(out totalRecord, pageRequest, type, group, grouptype);
                    break;
                default:
                    break;
            }
            pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = totalRecord,
                            sColumns = pageRequest.ColumnNames,
                            aaData = listCust
                        };
            return Json(pageResponse);
        }

        private List<object[]> CreateDatatableChuoi(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {

                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_chuoi" ? "chuoi" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Chuoi
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatableTrade(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group = null, string grouptype = null)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            totalRecord = customerRecords.Page == null ? 0 : customerRecords.Page.TotalRecords;
            type = type == "new_trade" ? "trade" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.BalTtqt,
                                                             x.BalBl,
                                                             x.DuNoTradeLoan,
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.BalTtqt,
                                                             x.BalBl,
                                                             x.DuNoTradeLoan,
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.BalTtqt,
                                                             x.BalBl,
                                                             x.DuNoTradeLoan,
                                                             x.Chuoi
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.BalTtqt,
                                                             x.BalBl,
                                                             x.DuNoTradeLoan,
                                                             x.Chuoi
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatableI2b(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_i2b" ? "i2b" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoUser,
                                                             x.Address
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoUser,
                                                             x.Address
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoUser,
                                                             x.Address
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoUser,
                                                             x.Address
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatablePos(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {

                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_pos" ? "pos" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoPos,
                                                             x.TransactionInMonth,
                                                             x.Address
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoPos,
                                                             x.TransactionInMonth,
                                                             x.Address
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoPos,
                                                             x.TransactionInMonth,
                                                             x.Address
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoPos,
                                                             x.TransactionInMonth,
                                                             x.Address
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatableCard(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.ScoredCard = pageRequest.SearchColumns[8];
                    filter.NoOfCard = pageRequest.SearchColumns[9];
                    filter.Balance = pageRequest.SearchColumns[10];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.ScoredCard = pageRequest.SearchColumns[7];
                    filter.NoOfCard = pageRequest.SearchColumns[8];
                    filter.Balance = pageRequest.SearchColumns[9];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.ScoredCard = pageRequest.SearchColumns[6];
                    filter.NoOfCard = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.ScoredCard = pageRequest.SearchColumns[4];
                    filter.NoOfCard = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_card" ? "card" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ScoredCard,
                                                             x.NoOfCard,
                                                             x.Balance,
                                                             x.Address
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ScoredCard,
                                                             x.NoOfCard,
                                                             x.Balance,
                                                             x.Address
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ScoredCard,
                                                             x.NoOfCard,
                                                             x.Balance,
                                                             x.Address
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ScoredCard,
                                                             x.NoOfCard,
                                                             x.Balance,
                                                             x.Address
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatableLoan(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_loan" ? "loan" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.NhomNo,
                                                             x.Address
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.NhomNo,
                                                             x.Address
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.NhomNo,
                                                             x.Address
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.NhomNo,
                                                             x.Address
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }

        private List<object[]> CreateDatatableDeposit(out int totalRecord, DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            List<object[]> result = new List<object[]>();
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = CreateCustomerProductRequest(pageRequest, filter, type, group, grouptype);
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
            }
            totalRecord = customerRecords.Page.TotalRecords;
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            type = type == "new_deposit" ? "deposit" : type;
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.Address,
                    }).ToList();
                    break;
                case "3":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.Address,
                    }).ToList();
                    break;
                case "2":
                    result = resultCustomer.Select(x => new object[]{ 
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.Address,
                    }).ToList();
                    break;
                case "1":
                    result = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName, 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Balance,
                                                             x.Address,
                    }).ToList();
                    break;
                default:
                    break;
            }
            return result;
        }
        #endregion

        #region Cutomer Priority
        [CustomActionFilter]
        [Authorize]
        public ActionResult CustPriority()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustPriorityData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        filter.Balance1 = pageRequest.SearchColumns[9];
                        filter.Balance2 = pageRequest.SearchColumns[10];
                        filter.CusStatus = pageRequest.SearchColumns[11];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        filter.Balance1 = pageRequest.SearchColumns[8];
                        filter.Balance2 = pageRequest.SearchColumns[9];
                        filter.CusStatus = pageRequest.SearchColumns[10];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        filter.Balance1 = pageRequest.SearchColumns[7];
                        filter.Balance2 = pageRequest.SearchColumns[8];
                        filter.CusStatus = pageRequest.SearchColumns[9];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        filter.Balance1 = pageRequest.SearchColumns[5];
                        filter.Balance2 = pageRequest.SearchColumns[6];
                        filter.CusStatus = pageRequest.SearchColumns[7];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerPriorityRequest request = new RetrieveCustomerPriorityRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerPriorityResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerPriority(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerPriorityRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.CusOpenDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Toi,
                                                             x.Toi2015,
                                                             x.NhomNo,
                                                             x.CustTypeName,
                                                             x.Cif
                                                           }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.CusOpenDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Toi,
                                                             x.Toi2015,
                                                             x.NhomNo,
                                                             x.CustTypeName,
                                                             x.Cif
                                                           }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.CusOpenDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Toi,
                                                             x.Toi2015,
                                                             x.NhomNo,
                                                             x.CustTypeName,
                                                             x.Cif
                                                           }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.CusOpenDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Toi,
                                                             x.Toi2015,
                                                             x.NhomNo,
                                                             x.CustTypeName,
                                                             x.Cif
                                                           }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = listCust
                        };
            }
            return Json(pageResponse);
        }
        #endregion
        #region CustSubSegment
        [CustomActionFilter]
        [Authorize]
        public ActionResult CustSubSegment()
        {
            var customerRecords = new CountCustSegmentResponse();
            var record = new CountCustSegmentRecord();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerSubSegmentRequest request = new RetrieveCustomerSubSegmentRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustSubSegment"
                    },
                    Page = new PageRecord(),
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                };
                customerRecords = serviceClient.Proxy.CountCustSegment(request);
                record = (customerRecords == null || customerRecords.Record == null) ? new CountCustSegmentRecord() : customerRecords.Record;
            }
            CustSubSegmentViewModel vm = new CustSubSegmentViewModel
            {
                CurrentUser = CurrentUserRecord,
                CountMme = record.CountMme,
                CountMsme = record.CountMsme,
                CountSme = record.CountSme
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustSubSegmentData(DataTablesPageRequest pageRequest, string type)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        break;
                    default:
                        break;
                }
                filter.SubSegment = type;
                RetrieveCustomerSubSegmentRequest request = new RetrieveCustomerSubSegmentRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustSubSegmentData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerSubSegmentResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerSubSegment(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                    }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                    }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                    }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                    }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = listCust
                        };
            }
            return Json(pageResponse);
        }
        #endregion
        #region New Customer
        [CustomActionFilter]
        [Authorize]
        public ActionResult NewCustomers()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult NewCustomerData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            var customerRecords = new RetrieveCustomerResponse();
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    break;
                default:
                    break;
            }
            if (string.IsNullOrEmpty(filter.CusOpenDate))
            {
                List<string> listDate = new List<string>();
                listDate.Add(DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy"));
                listDate.Add(DateTime.Now.ToString("dd/MM/yyyy"));
                filter.CusOpenDate = string.Join("-", listDate);
            }
            customerRecords = RetrieveAllCustomer(filter, pageRequest);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            List<object[]> listCust = new List<object[]>();
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "4":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                        }).ToList();
                    break;
                case "3":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                        }).ToList();
                    break;
                case "2":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                        }).ToList();
                    break;
                case "1":
                    listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                        }).ToList();
                    break;
                default:
                    break;
            }
            pageResponse = new DataTableResponse
                    {
                        sEcho = pageRequest.Echo,
                        iTotalRecords = customerRecords.Page.TotalRecords,
                        sColumns = pageRequest.ColumnNames,
                        aaData = listCust
                    };
            return Json(pageResponse);
        }
        #endregion

        #region Customer Feedback
        [CustomActionFilter]
        [Authorize]
        public ActionResult CustomerFeedback()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustFeedbackData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.Balance = pageRequest.SearchColumns[7];
                        filter.Balance1 = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.Balance = pageRequest.SearchColumns[6];
                        filter.Balance1 = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.Balance = pageRequest.SearchColumns[5];
                        filter.Balance1 = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.Balance = pageRequest.SearchColumns[3];
                        filter.Balance1 = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerFeedbackRequest request = new RetrieveCustomerFeedbackRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustFeedbackData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerFeedbackResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerFeedbackByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerFeedbackRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.Complaint,
                                                             x.Compliment
                                                           }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.Complaint,
                                                             x.Compliment
                                                           }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.Complaint,
                                                             x.Compliment
                                                           }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             CurrentLang.Equals("vi")? x.IndustryName: x.IndustryNameEn,
                                                             x.Complaint,
                                                             x.Compliment
                                                           }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listCust
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustFeedbackDetail(DataTablesPageRequest pageRequest, string cif)
        {
            List<CustomerFeedbackDetailRecord> resultCustomer = new List<CustomerFeedbackDetailRecord>();
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerFeedbackDetailByCifRequest request = new RetrieveCustomerFeedbackDetailByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustFeedbackDetail"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveCustomerFeedbackDetailByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerFeedbackDetailByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerFeedbackDetailRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.Title,
                                                             x.Creator,
                                                             x.Email1,
                                                             x.Assigned,
                                                             x.Modified,
                                                             x.Status1,
                                                             x.ServiceCategory,
                                                             x.Description,
                                                             x.Solution,
                                                             x.RootCause,
                                                             x.Created,
                                                             x.ModifiedDate
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Top 10
        [CustomActionFilter]
        [Authorize]
        public ActionResult Top10()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult Top10Data(DataTablesPageRequest pageRequest, string type)
        {
            DataTableResponse pageResponse = null;
            FilterRecord filterRecord = new FilterRecord();
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "3":
                    filterRecord.Dao = string.Join(",", GetListDaoManager().ToArray());
                    break;
                default:
                    break;
            }
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerTop10Request request = new RetrieveCustomerTop10Request
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.Top10Data"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 10,
                        PageIndex = 1
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filterRecord,
                    Type = int.Parse(type)
                };
                var customerRecords = serviceClient.Proxy.RetrieveCustomerTop10(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.Toi
                        }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{ 
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.Toi
                        }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{ 
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.Toi
                        }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{ 
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.Toi
                        }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = listCust
                        };
            }
            return Json(pageResponse);
        }
        #endregion
        #region Customers need to update information

        [CustomActionFilter]
        [Authorize]
        public ActionResult CustNeedUpdateInfo()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult CustNeedUpdateInfoData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerNeedToUpdateRequest request = new RetrieveCustomerNeedToUpdateRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerNeedToUpdateResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerNeedToUpdate(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoOfInfoNeedToUpdate,
                                                           }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoOfInfoNeedToUpdate,
                                                           }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoOfInfoNeedToUpdate,
                                                           }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch.Region,
                                                             x.Branch.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             CurrentLang.Equals("vi")? x.VpbIndustry: x.VpbIndustryEn,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.NoOfInfoNeedToUpdate,
                                                           }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                         {
                             sEcho = pageRequest.Echo,
                             iTotalRecords = customerRecords.Page.TotalRecords,
                             sColumns = pageRequest.ColumnNames,
                             aaData = listCust
                         };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustNeedUpdateInfoDetail(DataTablesPageRequest pageRequest, string cif)
        {
            List<CustomerNeedUpdateInfoRecord> resultCustomer = new List<CustomerNeedUpdateInfoRecord>();
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerNeedUpdateInfoRawRequest request = new RetrieveCustomerNeedUpdateInfoRawRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustFeedbackDetail"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = new FilterRecord
                    {
                        Cif = cif
                    }
                };
                var customerRecords = new RetrieveCustomerNeedUpdateInfoRawResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerNeedUpdateInfoRawByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerNeedUpdateInfoRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.InfoName,
                                                             x.ValueInfo,
                                                             x.InfoDescription
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion
        #region Customer Low Value
        [Authorize]
        [CustomActionFilter]
        public ActionResult CustomerLowValue(string cif)
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult CustomerLowValueData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerLowValueRequest request = new RetrieveCustomerLowValueRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.NewCustomerData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerLowValueResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerLowValue(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerLowValueRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.IndustryName,
                                                             x.CusOpenDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.AvgToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.CustomerValue,
                                                             x.Cif
                        }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.IndustryName,
                                                             x.CusOpenDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.AvgToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.CustomerValue,
                                                             x.Cif
                        }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.IndustryName,
                                                             x.CusOpenDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.AvgToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.CustomerValue,
                                                             x.Cif
                        }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Region,
                                                             x.BranchName,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.IndustryName,
                                                             x.CusOpenDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.AvgToi2016.ToString("#,###", CultureInfo.GetCultureInfo("vi-VN").NumberFormat),
                                                             x.CustomerValue,
                                                             x.Cif
                        }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                         {
                             sEcho = pageRequest.Echo,
                             iTotalRecords = customerRecords.Page.TotalRecords,
                             sColumns = pageRequest.ColumnNames,
                             aaData = listCust
                         };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustomerToiInfoData(string cif)
        {
            CustomerReduceFeeInfoRecord resultCustomer = new CustomerReduceFeeInfoRecord();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerReduceInfoRequest request = new RetrieveCustomerReduceInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ReduceRateFee.CustomerToiInfoData"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerReduceInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerReduceInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new CustomerReduceFeeInfoRecord() : customerRecords.Record;
            }
            return Json(resultCustomer);
        }

        #endregion
        #region Freedom Customer

        [CustomActionFilter]
        [Authorize]
        public ActionResult FreedomCustomer()
        {
            CustomerPortfolioViewModel vm = new CustomerPortfolioViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }


        [Authorize]
        [HttpPost]
        public ActionResult FreedomCustomerData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.Region = pageRequest.SearchColumns[0];
                filter.Branch = pageRequest.SearchColumns[1];
                filter.Dao = pageRequest.SearchColumns[2];
                filter.DaoName = pageRequest.SearchColumns[3];
                filter.Cif = pageRequest.SearchColumns[4];
                filter.CustName = pageRequest.SearchColumns[5];
                filter.Industry = pageRequest.SearchColumns[6];
                filter.CusOpenDate = pageRequest.SearchColumns[7];
                filter.CusStatus = pageRequest.SearchColumns[8];
                RetrieveCustomerFreedomRequest request = new RetrieveCustomerFreedomRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerFreedomResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerFreedom(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerFreedomRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CusName,
                                                             x.IndustryName,
                                                             x.StartCurStatus.HasValue? x.StartCurStatus.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):"",
                                                             x.Status,
                                                           }).ToList()
                };
            }
            return Json(pageResponse);
        }

        #endregion
        #region Ajax Methods
        [Authorize]
        [HttpPost]
        public ActionResult CustomerI2BDetailData(DataTablesPageRequest pageRequest, string cif)
        {
            List<CustomerI2BDetail> resultCustomer = new List<CustomerI2BDetail>();
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerI2bDetailByCifRequest request = new RetrieveCustomerI2bDetailByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerI2BDetailData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveCustomerI2bDetailByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerI2bDetailByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerI2BDetail>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             cif,
                                                             x.UserName,
                                                             x.Status
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult CustomerCardDetailData(DataTablesPageRequest pageRequest, string cif)
        {
            List<CustomerCardDetailRecord> resultCustomer = new List<CustomerCardDetailRecord>();
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerCardDetailByCifRequest request = new RetrieveCustomerCardDetailByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerCardDetailData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = new FilterRecord
                    {
                        Cif = cif
                    }
                };
                var customerRecords = new RetrieveCustomerCardDetailByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerCardDetailByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerCardDetailRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.ContractNumber,
                                                             x.CardNumber,
                                                             x.Balance,
                                                             x.TypeCard,
                                                             (-1* x.CardLimit).ToString("##,##0.00"),
                                                             x.UnlockCard,
                                                             x.UnlockDate.HasValue? x.UnlockDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty,
                                                             x.ActualDateExpire.HasValue? x.ActualDateExpire.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty,
                                                             x.ScoredCard
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ListIndustry(string data)
        {
            List<BranchRecord> resultCustomer = new List<BranchRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveListBranchRequest request = new RetrieveListBranchRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.ListBranch"
                    },
                    Dao = CurrentUserRecord
                };
                var customerRecords = new RetrieveListBranchResponse();
                customerRecords = serviceClient.Proxy.RetrieveListBranch(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<BranchRecord>() : customerRecords.Records;
            }
            JsonResult result = new JsonResult();
            var objectItem = resultCustomer.Select(x => new
            {
                id = x.BranchId,
                text = x.BranchName
            });
            result = Json(objectItem);
            return result;
        }

        [Authorize]
        [HttpPost]
        public ActionResult ListGroupItem(string type, string product)
        {
            List<ListProductRecord> resultCustomer = new List<ListProductRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveListProductRequest request = new RetrieveListProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.ListGroupItem"
                    },
                    Dao = CurrentUserRecord.Dao,
                    Type = product,
                    Group = type
                };
                var customerRecords = new RetrieveListProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveListProduct(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ListProductRecord>() : customerRecords.Records;
            }
            JsonResult result = new JsonResult();
            switch (product)
            {
                case "deposit":
                    var objectItem = resultCustomer.Select(x => new
                    {
                        id = x.ProductDeposit,
                        text = x.ProductDeposit
                    });
                    result = Json(objectItem);
                    break;
                case "loan":
                    var objectLoanItem = resultCustomer.Select(x => new
                    {
                        id = x.ProductLoan,
                        text = x.ProductLoan
                    });
                    result = Json(objectLoanItem);
                    break;
                default:
                    break;
            }

            return result;
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustomerInfo(string cif)
        {
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerInfo"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                address = x.Address,
                custName = x.CustName,
                cif = cif,
                email = x.Email,
                phone = x.Phone,
                office = x.Office,
                landLine = x.LandLine,
                fax = x.Fax,
                industry = x.VbpIndustry
            });
            return Json(objectItem);
        }

        [Authorize]
        [HttpPost]
        public ActionResult NewLending(DataTablesPageRequest pageRequest, string cif)
        {
            DataTableResponse pageResponse = null;
            List<NewLendingRecord> resultCustomer = new List<NewLendingRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveNewLendingInfoRequest request = new RetrieveNewLendingInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.NewLending"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveNewLendingInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveNewLendingInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<NewLendingRecord>() : customerRecords.Records;
            }
            pageResponse = new DataTableResponse
            {
                sEcho = pageRequest.Echo,
                sColumns = pageRequest.ColumnNames,
                aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.Cif,
                                                                x.CustName,
                                                                x.ChuongTrinhSp,
                                                                x.DistDate,
                                                                x.Acctno,
                                                                x.MatDate
                    }).ToList()
            };
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RepreInfo(string repreCif)
        {
            List<RelCusInfoRecord> resultCustomer = new List<RelCusInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveRelCusInfoRequest request = new RetrieveRelCusInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    RelCif = repreCif
                };
                var customerRecords = new RetrieveRelCusInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRelCusInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RelCusInfoRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                address = x.RelCusAddress,
                custName = x.RelCusName,
                cif = repreCif,
                email = x.RelCusEmail,
                phone = x.RelCusMobile,
                gender = x.RelCusGender,
                position = x.RelCusPosition
            });
            return Json(objectItem);
        }
        #endregion

        #region Helper Methods
        [Authorize]
        private CustomerPortfolioViewModel CountCustomerByGroup(bool isNew = false)
        {
            CustomerPortfolioViewModel result = new CustomerPortfolioViewModel();
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                RetrieveCustomerCountByGroupRequest request = new RetrieveCustomerCountByGroupRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CountCustomerByGroup"
                    },
                    Dao = CurrentUserRecord,
                    IsNew = isNew
                };
                var listGroupRecords = serviceClient.Proxy.RetrieveCustomerCountByGroup(request);
                result.listGroupRecord = (listGroupRecords == null || listGroupRecords.Records == null) ? new List<CustomerGroupCountRecord>() : listGroupRecords.Records;
            }
            return result;
        }


        [Authorize]
        [HttpPost]
        public ActionResult GetListAllCustomer(string searchText)
        {
            List<CustomerRecord> resultCustomer = new List<CustomerRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord
                {
                    TextSearchByCif = searchText,
                    RequestStatus = "Y"
                };
                RetrieveListCustomerRequest request = new RetrieveListCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveListCustomerResponse();
                customerRecords = serviceClient.Proxy.RetrieveListCustomer(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                id = x.CIF,
                text = x.CIF,
                custName = x.Name,
                legalId = x.LegalId
            });
            return Json(objectItem);
        }

        [Authorize]
        [HttpPost]
        public ActionResult GetListCustomer(string searchText)
        {
            List<CustomerRecord> resultCustomer = new List<CustomerRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord
                {
                    TextSearchByCif = searchText
                };
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "2":
                        filter.Region = CurrentUserRecord.Zone;
                        break;
                    case "3":
                        filter.Branch = CurrentUserRecord.BranchId;
                        break;
                    case "4":
                        filter.Dao = CurrentUserRecord.Dao;
                        break;
                    default:
                        break;
                }
                RetrieveListCustomerRequest request = new RetrieveListCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveListCustomerResponse();
                customerRecords = serviceClient.Proxy.RetrieveListCustomer(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                id = x.CIF,
                text = x.CIF,
                custName = x.Name,
                legalId = x.LegalId
            });
            return Json(objectItem);
        }

        [Authorize]
        private RetrieveCustomerResponse RetrieveAllCustomer(FilterRecord filter, DataTablesPageRequest pageRequest = null)
        {
            var customerRecords = new RetrieveCustomerResponse();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = new RetrieveCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.TotalCustomerData"
                    },
                    Page = new PageRecord
                        {
                            PageSize = pageRequest == null ? 0 : pageRequest.DisplayLength,
                            PageIndex = pageRequest == null ? 0 : pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                        },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                customerRecords = serviceClient.Proxy.RetrieveTotalCustomer(request);
            }
            return customerRecords;
        }

        [Authorize]
        private RetrieveCustomerRequest CreateCustomerProductRequest(DataTablesPageRequest pageRequest, FilterRecord filter, string type, string group = null, string grouptype = null)
        {
            var customerRequest = new RetrieveCustomerRequest
            {
                Header = new RequestHeaderRecord
                {
                    ApplicationKey = SystemConfiguration.ApplicationKey,
                    CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                },
                Page = new PageRecord
                {
                    PageSize = pageRequest.DisplayLength,
                    PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                },
                CurrentUser = CurrentUserRecord,
                Lang = CurrentLang,
                Filter = filter,
                Type = type,
                FromDate = DateTime.Now.AddMonths(-1),
                Group = string.IsNullOrEmpty(group) || group.Equals("null") ? string.Empty : group,
                GroupType = string.IsNullOrEmpty(grouptype) || grouptype.Equals("null") ? string.Empty : grouptype
            };
            return customerRequest;
        }
        #endregion
    }
}
